package component

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.unit.dp
import com.lt.load_the_image.rememberImagePainter
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

/**
 * @description 底部Banner
 * @author      jankinwu
 * @date        2024/4/13 0:24
 */
@Serializable
data class BannerDTO(
    val display: Boolean = true,
)

var bannerDTO = mutableStateOf(BannerDTO());

@Composable
fun Banner(windowHeight: Int) {
    Box(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Image(
            painter = rememberImagePainter("image/banner4.png"),
            contentDescription = "",
            modifier = Modifier
                .fillMaxWidth()
                .height((windowHeight / 3.8).dp)
                .offset(y = (18).dp)
                .align(Alignment.BottomCenter),
            contentScale = ContentScale.FillBounds
        )
    }
}

@Composable
fun BannerScreen(windowHeight: Int) {
    val bannerDTOState by remember { mutableStateOf(bannerDTO) }
    var windowHeightState by remember { mutableStateOf(windowHeight) }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .onGloballyPositioned {
                windowHeightState = it.size.height
            }
    ) {
        if (bannerDTOState.value.display) {
            Banner(windowHeightState)
        }
    }
}

fun handleBanner(data: String) {
    bannerDTO.value = Json.decodeFromString<BannerDTO>(data)
}